package Servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import Services.RegisterService;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public RegisterServlet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		//
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String fname = request.getParameter("fname");
		String lname = request.getParameter("lname");
		String email = request.getParameter("email");
		// Tests
		System.out.println(username);
		System.out.println(password);
		System.out.println(fname);
		System.out.println(lname);
		System.out.println(email);
		//
		RegisterService registerservice = new RegisterService();
		int returncode = registerservice.sendRegistrationRequest(username,
				password, email, fname, lname);
		String registerMessage = "Register Service Failed";
		switch (returncode) {
		case 1:
			response.sendRedirect("/Pages/index.jsp");
			break;
		case 2:
			registerMessage = "Username already in use";
			System.out.println(registerMessage);
			session.setAttribute("registermessage", registerMessage);
			response.sendRedirect("/Pages/registerError.jsp");
			break;
		case 3:
			registerMessage = "Email already in use";
			System.out.println(registerMessage);
			session.setAttribute("registermessage", registerMessage);
			response.sendRedirect("/Pages/registerError.jsp");
			break;
		default:
			session.setAttribute("registermessage", registerMessage);
			response.sendRedirect("Pages/registerError.jsp");
			break;
		}

	}
}
